package com.tywj.buscustomerapp;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.multidex.MultiDex;

//import com.facebook.drawee.backends.pipeline.Fresco;

import com.tywj.buscustomerapp.utils.MyActivityManager;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;

import java.util.List;

/**
 * Created by taylorjoe on 17-11-15.
 */

public class App extends Application {
    private static Context sInstance;
    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        UMConfigure.init(getApplicationContext(),UMConfigure.DEVICE_TYPE_PHONE, "fdfe6c8b50814c82efe02aa93f3c7bc6");
        UMConfigure.setLogEnabled(false);
        MobclickAgent.openActivityDurationTrack(false);
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
                MyActivityManager.getInstance().setCurrentActivity(activity);
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
        PushAgent mPushAgent = PushAgent.getInstance(this);
        mPushAgent.register(new IUmengRegisterCallback() {
            @Override
            public void onSuccess(String deviceToken) {
//                Log.i("APPTAY",deviceToken);
            }
            @Override
            public void onFailure(String s, String s1) {
//                Log.i("APPTAY",s+":"+s1);
            }
        });
//        getAppList();
    }

    public static Context getContext(){
        return sInstance;
    }

    private void getAppList() {
        PackageManager pm = getPackageManager();
        List<PackageInfo> packages = pm.getInstalledPackages(0);
        for (PackageInfo packageInfo : packages) {
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) // 非系统应用
            {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    System.out.println("获取到的=" + packageInfo.splitNames);
                }
                System.out.println("获取到的=" + packageInfo.packageName);
            }
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }
}